package annotate.encode;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SearchGivenChromosomeInterval {
	
	List<RegionofInterest> regionofInterest = new ArrayList<RegionofInterest>();
	
	
	
//	This program reads SNPs(chromosome number and coordinate on chromosome) or
//	region of interest (chromosome number, start and end position) from input file
	
//	It later searches on the related sorted chromosome base tfbs file
//	whether there is any tfbs in the searched region of the interest or SNP on the given chromosomic position
	
	
//	read input file
	public void readIntervals(String fileName){
		
		  	FileWriter fileWriter = null;	
		  	FileReader fileReader =null;

			try {
				fileReader= new FileReader(fileName);
				fileWriter =  new FileWriter(Commons.SEARCH_OUTPUT_FILE);
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			BufferedReader br = new BufferedReader(fileReader);
			BufferedWriter bw =new BufferedWriter(fileWriter);

			String strLine;
			
			int indexofFirstTab = 0;
			int indexofSecondTab = 0;
						
			String chromName;
			int startPosition;
			int endPosition = 0;
			int position;
			
			RegionofInterest regionofInterest = new RegionofInterest();
			
			  try {
//				  read the region of interest from an input file line by one
				while ((strLine = br.readLine()) != null)   {
					//data can be two column or three column 					
					//in two column case: get the SNP data
					//in three column case: get the interval data (region of interest)
					
					indexofFirstTab = strLine.indexOf('\t');
					indexofSecondTab = strLine.indexOf('\t', indexofFirstTab+1);
					
					if (indexofSecondTab >=0){
//					three input data
//					we will search for region of interest on a chromosome
						chromName =strLine.substring(0,indexofFirstTab);						
						startPosition = Integer.parseInt(strLine.substring(indexofFirstTab+1,indexofSecondTab));
						try{
							endPosition = Integer.parseInt(strLine.substring(indexofSecondTab+1));
						}catch(NumberFormatException e){
							System.out.println(strLine.substring(indexofSecondTab+1));
							System.out.println(endPosition);
						}
			
						regionofInterest.setChromName(chromName);
						regionofInterest.setStartPosition(startPosition);
						regionofInterest.setEndPosition(endPosition);
						
					}else{
//				 	two input data
//					we will search for a SNP on a chromosome
						chromName = strLine.substring(0,indexofFirstTab);
						position = Integer.parseInt(strLine.substring(indexofFirstTab+1));
						
						regionofInterest.setChromName(chromName);
						regionofInterest.setStartPosition(position);
						regionofInterest.setEndPosition(position);				
					
					}
//					search for each region of interest
					bw.write("Search for"+ "\t" +chromName +"\t"+ regionofInterest.getStartPosition()+ "\t" + regionofInterest.getEndPosition()+ "\n");

//					whether there is any tbfs in this region of interest
					searchforTfbsintheGivenRegionofInterest(regionofInterest,bw);
//					whether there is any histone in this region of interest
					searchforHistoneintheGivenRegionofInterest(regionofInterest,bw);
//					whether there is any dnase in this region of interest
					searchforDnaseintheGivenRegionofInterest(regionofInterest,bw);
//					whether there are genes in this region of interest
					searchforRefSeqGenesintheGivenRegionofInterest(regionofInterest,bw);
				  }
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
//				close the outermost reader and writer in the chain.
//				In this case BufferedReader and BufferedWriter
				br.close();
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}

//	Does the given region of interest intersect with the any tfbs
	public boolean intersect(int startPosition,int endPosition,int startPositionofRegionofInterest, int endPositionofRegionofInterest){
		
		int startPositionofIntersection = Math.max(startPosition, startPositionofRegionofInterest);
		int endPositionofIntersection = Math.min(endPosition, endPositionofRegionofInterest);
		
		if (startPositionofIntersection <= endPositionofIntersection)
			return true;
		else 
			return false;
	}
	
//	search whether there is any tfbs in region of interest
//	search whether there is any histone modifications in the region of interest
//	search whether there is any dnase in the region of interest
//	search whether there are any genes in the region of interest
	public void searchforTfbsintheGivenRegionofInterest(RegionofInterest region, BufferedWriter bw){
		String chromName = region.getChromName();
		int startPositionofRegionofInterest = region.getStartPosition();
		int endPositionofRegionofInterest = region.getEndPosition();
		
		FileReader fileReader =null;
		BufferedReader br = null;
		
		String strLine;		
		
		int startPosition = 0;
		int endPosition = 0;
		String tfName;
		String  cellLineName;
		String fileName;
		
		int indexofFirstTab = 0;
		int indexofSecondTab = 0;
		int indexofThirdTab = 0;
		int indexofFourthTab = 0;
		int indexofFifthTab = 0;
		
		try {
			if (chromName.equals(Commons.CHROMOSOME1)){
				//open sorted tfbs file of Chromosome1
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR1_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME2)){
				//open sorted tfbs file of Chromosome2
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR2_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME3)){
				//open sorted tfbs file of Chromosome3
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR3_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME4)){
				//open sorted tfbs file of Chromosome4
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR4_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME5)){
				//open sorted tfbs file of Chromosome5
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR5_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME6)){
				//open sorted tfbs file of Chromosome6
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR6_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME7)){
				//open sorted tfbs file of Chromosome7
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR7_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME8)){
				//open sorted tfbs file of Chromosome8
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR8_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME9)){
				//open sorted tfbs file of Chromosome9
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR9_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME10)){
				//open sorted tfbs file of Chromosome10
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR10_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME11)){
				//open sorted tfbs file of Chromosome11
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR11_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME12)){
				//open sorted tfbs file of Chromosome12
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR12_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME13)){
				//open sorted tfbs file of Chromosome13
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR13_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME14)){
				//open sorted tfbs file of Chromosome14
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR14_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME15)){
				//open sorted tfbs file of Chromosome15
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR15_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME16)){
				//open sorted tfbs file of Chromosome16
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR16_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME17)){
				//open sorted tfbs file of Chromosome17
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR17_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME18)){
				//open sorted tfbs file of Chromosome18
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR18_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME19)){
				//open sorted tfbs file of Chromosome19
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR19_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME20)){
				//open sorted tfbs file of Chromosome20
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR20_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME21)){
				//open sorted tfbs file of Chromosome21
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR21_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOME22)){
				//open sorted tfbs file of Chromosome22
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR22_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOMEX)){
				//open sorted tfbs file of ChromosomeX
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRX_TFBS);
				
			}else if (chromName.equals(Commons.CHROMOSOMEY)){
				//open sorted tfbs file of ChromosomeY
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRY_TFBS);
				
			}	
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 br = new BufferedReader(fileReader);
		 
		 
			try {
				
//				Data in the Sorted_Chromosome_Base_Tfbs_File is in ascending order for start position field
//				A custom tfbs line
//				chrY	2649586	2649842	Pol2	Hepg2	spp.optimal.wgEncodeHaibTfbsHepg2Pol2Pcr2xAlnRep0_VS_wgEncodeHaibTfbsHepg2ControlPcr2xAlnRep0.narrowPeak

				while ((strLine = br.readLine()) != null)   {
					indexofFirstTab = strLine.indexOf('\t');
					indexofSecondTab = strLine.indexOf('\t', indexofFirstTab+1);
					indexofThirdTab = strLine.indexOf('\t', indexofSecondTab+1);
					indexofFourthTab = strLine.indexOf('\t', indexofThirdTab+1);
					indexofFifthTab = strLine.indexOf('\t', indexofFourthTab+1);
															
					startPosition = Integer.parseInt(strLine.substring(indexofFirstTab+1, indexofSecondTab));
					endPosition = Integer.parseInt(strLine.substring(indexofSecondTab+1, indexofThirdTab));
					
//					Since the data is sorted with respect to start position field in ascending order
//					if start position of the region of interest is greater than the end position in the  Sorted_Chromosome_Base_Tfbs_File
//					They can not intersect
//					Also there can not be any intersection for the rest of the data
					if (endPositionofRegionofInterest < startPosition)
						break;
					
					if (intersect(startPosition,endPosition,startPositionofRegionofInterest, endPositionofRegionofInterest)){
//						write this tfbs to output file

//						get the data
						tfName = strLine.substring(indexofThirdTab+1, indexofFourthTab);
						cellLineName = strLine.substring(indexofFourthTab+1, indexofFifthTab);
						fileName = strLine.substring(indexofFifthTab+1);

						bw.write("tfbs" + "\t" + tfName + "\t" + startPosition + "\t" + endPosition  + "\t" + cellLineName + "\t"+ fileName + "\n");
					}
					
				}
				
//				close the buffered reader
				br.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				
		 

	}
	
	

	public void searchforRefSeqGenesintheGivenRegionofInterest(RegionofInterest region,BufferedWriter bw){
		String chromName = region.getChromName();
		int startPositionofRegionofInterest = region.getStartPosition();
		int endPositionofRegionofInterest = region.getEndPosition();
		
		FileReader fileReader =null;
		BufferedReader br = null;
		
		String strLine;		
		
		int startPosition = 0;
		int endPosition = 0;
		
		String  refSeqGeneName;
		Integer geneEntrezId;
		String intervalName;
//		char strand;
		String geneHugoSymbol;
		
		
		int indexofFirstTab = 0;
		int indexofSecondTab = 0;
		int indexofThirdTab = 0;
		int indexofFourthTab = 0;
		int indexofFifthTab = 0;
		int indexofSixthTab = 0;
		int indexofSeventhTab = 0;
		
		
		try {
			if (chromName.equals(Commons.CHROMOSOME1)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR1_REFSEQ_GENES);				
			}else if (chromName.equals(Commons.CHROMOSOME2)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR2_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME3)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR3_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME4)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR4_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME5)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR5_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME6)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR6_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME7)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR7_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME8)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR8_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME9)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR9_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME10)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR10_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME11)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR11_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME12)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR12_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME13)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR13_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME14)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR14_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME15)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR15_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME16)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR16_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME17)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR17_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME18)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR18_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME19)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR19_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME20)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR20_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME21)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR21_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOME22)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHR22_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOMEX)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHRX_REFSEQ_GENES);			
			}else if (chromName.equals(Commons.CHROMOSOMEY)){
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_UCSCGENOME_SORTED_CHRY_REFSEQ_GENES);			
			}			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		 br = new BufferedReader(fileReader);

			try {
				
//				Data in the Sorted_Chromosome_Base_RefSeq_Gene_File is in ascending order for interval start field
//				a custom line from Sorted Chromosome Base Refseq Gene File
//              chr4	154701	156490	NM_001039127	255403	Exon4	+	ZNF718
//				chr14	19277593	19367592	NM_001013354	440153	5p3	+	OR11H12


				while ((strLine = br.readLine()) != null)   {
					indexofFirstTab = strLine.indexOf('\t');
					indexofSecondTab = strLine.indexOf('\t', indexofFirstTab+1);
					indexofThirdTab = strLine.indexOf('\t', indexofSecondTab+1);
					indexofFourthTab = strLine.indexOf('\t', indexofThirdTab+1);
					indexofFifthTab = strLine.indexOf('\t', indexofFourthTab+1);
					indexofSixthTab = strLine.indexOf('\t', indexofFifthTab+1);
					indexofSeventhTab = strLine.indexOf('\t',indexofSixthTab+1);										
					
					startPosition = Integer.parseInt(strLine.substring(indexofFirstTab+1,indexofSecondTab));
					endPosition = Integer.parseInt(strLine.substring(indexofSecondTab+1, indexofThirdTab));
					
//					Since the data is sorted with respect to start position field in ascending order
//					if start position of the region of interest is greater than the end position in the  Sorted_Chromosome_Base_Dnase_File
//					They can not intersect
//					Also there can not be any intersection for the rest of the data
					if (endPositionofRegionofInterest < startPosition)
						break;
					
					if (intersect(startPosition,endPosition,startPositionofRegionofInterest, endPositionofRegionofInterest)){
//						write this refseq gene to output file

//						get the data
						refSeqGeneName = strLine.substring(indexofThirdTab+1, indexofFourthTab);
						geneEntrezId = Integer.parseInt(strLine.substring(indexofFourthTab+1,indexofFifthTab));
						intervalName = strLine.substring(indexofFifthTab+1,indexofSixthTab);
						geneHugoSymbol = strLine.substring(indexofSeventhTab+1);
						
						bw.write("refSeqGene" + "\t" + refSeqGeneName + "\t" + startPosition + "\t" + endPosition + "\t"  + intervalName + "\t" + geneHugoSymbol + "\t" + geneEntrezId +"\n");
					}
					
				}
				
//				close the buffered reader
				br.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				

		 
	}

	

//	search whether there is any dnase in the region of interest
	public void	searchforDnaseintheGivenRegionofInterest(RegionofInterest region, BufferedWriter bw){
		String chromName = region.getChromName();
		int startPositionofRegionofInterest = region.getStartPosition();
		int endPositionofRegionofInterest = region.getEndPosition();
		
		FileReader fileReader =null;
		BufferedReader br = null;
		
		String strLine;		
		
		int startPosition = 0;
		int endPosition = 0;
		String  cellLineName;
		String fileName;
		
		int indexofFirstTab = 0;
		int indexofSecondTab = 0;
		int indexofThirdTab = 0;
		int indexofFourthTab = 0;
		
		try {
			if (chromName.equals(Commons.CHROMOSOME1)){
				//open sorted dnase file of Chromosome1
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR1_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME2)){
				//open sorted dnase file of Chromosome2
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR2_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME3)){
				//open sorted dnase file of Chromosome3
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR3_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME4)){
				//open sorted dnase file of Chromosome4
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR4_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME5)){
				//open sorted dnase file of Chromosome5
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR5_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME6)){
				//open sorted dnase file of Chromosome6
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR6_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME7)){
				//open sorted dnase file of Chromosome7
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR7_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME8)){
				//open sorted dnase file of Chromosome8
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR8_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME9)){
				//open sorted dnase file of Chromosome9
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR9_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME10)){
				//open sorted dnase file of Chromosome10
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR10_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME11)){
				//open sorted dnase file of Chromosome11
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR11_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME12)){
				//open sorted dnase file of Chromosome12
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR12_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME13)){
				//open sorted dnase file of Chromosome13
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR13_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME14)){
				//open sorted dnase file of Chromosome14
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR14_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME15)){
				//open sorted dnase file of Chromosome15
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR15_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME16)){
				//open sorted dnase file of Chromosome16
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR16_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME17)){
				//open sorted dnase file of Chromosome17
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR17_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME18)){
				//open sorted dnase file of Chromosome18
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR18_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME19)){
				//open sorted dnase file of Chromosome19
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR19_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME20)){
				//open sorted dnase file of Chromosome20
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR20_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME21)){
				//open sorted dnase file of Chromosome21
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR21_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOME22)){
				//open sorted dnase file of Chromosome22
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR22_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOMEX)){
				//open sorted dnase file of ChromosomeX
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRX_DNASE);
				
			}else if (chromName.equals(Commons.CHROMOSOMEY)){
				//open sorted dnase file of ChromosomeY
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRY_DNASE);
				
			}	
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		 br = new BufferedReader(fileReader);
		 
		 
			try {
				
//				Data in the Sorted_Chromosome_Base_Dnase_File is in ascending order for start position field
//				a custom line from Sorted Chromosome Base Dnase File
//				chr4	10000	10150	AG04450	AG04450-DS12255.peaks.fdr0.01.hg19.bed

				while ((strLine = br.readLine()) != null)   {
					indexofFirstTab = strLine.indexOf('\t');
					indexofSecondTab = strLine.indexOf('\t', indexofFirstTab+1);
					indexofThirdTab = strLine.indexOf('\t', indexofSecondTab+1);
					indexofFourthTab = strLine.indexOf('\t', indexofThirdTab+1);
					
					
					startPosition = Integer.parseInt(strLine.substring(indexofFirstTab+1,indexofSecondTab));
					endPosition = Integer.parseInt(strLine.substring(indexofSecondTab+1, indexofThirdTab));
					
//					Since the data is sorted with respect to start position field in ascending order
//					if start position of the region of interest is greater than the end position in the  Sorted_Chromosome_Base_Dnase_File
//					They can not intersect
//					Also there can not be any intersection for the rest of the data
					if (endPositionofRegionofInterest < startPosition)
						break;
					
					if (intersect(startPosition,endPosition,startPositionofRegionofInterest, endPositionofRegionofInterest)){
//						write this dnase to output file

//						get the data
						cellLineName = strLine.substring(indexofThirdTab+1, indexofFourthTab);
						fileName = strLine.substring(indexofFourthTab+1);

						bw.write("dnase" + "\t" + "-" + "\t" +startPosition + "\t" + endPosition + "\t"  + cellLineName + "\t"+ fileName + "\n");
					}
					
				}
				
//				close the buffered reader
				br.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				
	}


	
//	search whether there is any histone modifications in the region of interest
	public void searchforHistoneintheGivenRegionofInterest(RegionofInterest region, BufferedWriter bw){
		String chromName = region.getChromName();
		int startPositionofRegionofInterest = region.getStartPosition();
		int endPositionofRegionofInterest = region.getEndPosition();
		
		FileReader fileReader =null;
		BufferedReader br = null;
		
		String strLine;		
		
		int startPosition = 0;
		int endPosition = 0;
		String histoneName;
		String  cellLineName;
		String fileName;
		
		int indexofFirstTab = 0;
		int indexofSecondTab = 0;
		int indexofThirdTab = 0;
		int indexofFourthTab = 0;
		int indexofFifthTab = 0;
		
		
		try {
			if (chromName.equals(Commons.CHROMOSOME1)){
				//open sorted histone file of Chromosome1
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR1_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME2)){
				//open sorted histone file of Chromosome2
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR2_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME3)){
				//open sorted histone file of Chromosome3
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR3_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME4)){
				//open sorted histone file of Chromosome4
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR4_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME5)){
				//open sorted histone file of Chromosome5
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR5_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME6)){
				//open sorted histone file of Chromosome6
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR6_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME7)){
				//open sorted histone file of Chromosome7
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR7_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME8)){
				//open sorted histone file of Chromosome8
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR8_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME9)){
				//open sorted histone file of Chromosome9
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR9_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME10)){
				//open sorted histone file of Chromosome10
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR10_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME11)){
				//open sorted histone file of Chromosome11
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR11_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME12)){
				//open sorted histone file of Chromosome12
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR12_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME13)){
				//open sorted histone file of Chromosome13
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR13_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME14)){
				//open sorted histone file of Chromosome14
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR14_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME15)){
				//open sorted histone file of Chromosome15
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR15_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME16)){
				//open sorted histone file of Chromosome16
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR16_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME17)){
				//open sorted histone file of Chromosome17
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR17_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME18)){
				//open sorted histone file of Chromosome18
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR18_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME19)){
				//open sorted histone file of Chromosome19
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR19_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME20)){
				//open sorted histone file of Chromosome20
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR20_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME21)){
				//open sorted histone file of Chromosome21
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR21_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOME22)){
				//open sorted histone file of Chromosome22
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR22_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOMEX)){
				//open sorted histone file of ChromosomeX
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRX_HISTONE);
				
			}else if (chromName.equals(Commons.CHROMOSOMEY)){
				//open sorted histone file of ChromosomeY
				fileReader = new FileReader(Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRY_HISTONE);
				
			}	
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 br = new BufferedReader(fileReader);
		 
		 
			try {
				
//				Data in the Sorted_Chromosome_Base_Histone_File is in ascending order for start position field
// 				a custom line from Sorted Chromosome Base Histone File
//				chrY	2649445	2650904	H3k27ac	Hepg2	wgEncodeBroadHistoneHepg2H3k27acStdAln.narrowPeak

				while ((strLine = br.readLine()) != null)   {
					indexofFirstTab = strLine.indexOf('\t');
					indexofSecondTab = strLine.indexOf('\t', indexofFirstTab+1);
					indexofThirdTab = strLine.indexOf('\t', indexofSecondTab+1);
					indexofFourthTab = strLine.indexOf('\t', indexofThirdTab+1);
					indexofFifthTab = strLine.indexOf('\t', indexofFourthTab+1);
					
					
					startPosition = Integer.parseInt(strLine.substring(indexofFirstTab+1,indexofSecondTab));
					endPosition = Integer.parseInt(strLine.substring(indexofSecondTab+1, indexofThirdTab));
					
//					Since the data is sorted with respect to start position field in ascending order
//					if start position of the region of interest is greater than the end position in the  Sorted_Chromosome_Base_Histone_File
//					They can not intersect
//					Also there can not be any intersection for the rest of the data
					if (endPositionofRegionofInterest < startPosition)
						break;
					
					if (intersect(startPosition,endPosition,startPositionofRegionofInterest, endPositionofRegionofInterest)){
//						write this histone to output file

//						get the data
						histoneName = strLine.substring(indexofThirdTab+1, indexofFourthTab);
						cellLineName = strLine.substring(indexofFourthTab+1, indexofFifthTab);
						fileName = strLine.substring(indexofFifthTab+1);

//						todo		bw.write("tfbs" + "\t" + tfName + "\t" + startPosition + "\t" + endPosition  + "\t" + cellLineName + "\t"+ fileName + "\n");

												
						bw.write("histone" + "\t" + histoneName + "\t" + startPosition + "\t" + endPosition + "\t" + cellLineName + "\t" + fileName + "\n");
					}
					
				}
				
//				close the buffered reader
				br.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				
		 

	}

	
			
	public static void main(String[] args){
		SearchGivenChromosomeInterval search = new SearchGivenChromosomeInterval();
		
//		Read the input file to a list
		search.readIntervals(args[0]);
		
		
		
	}
}
